import {
  addItemToCollect,
  checkUsername,
  checkPhone,
  deleteItemfromCollect,
  getAllCollectbyId,
  getCertainCollect,
  getAccount,
  signOutAccount,
  loginAccount,
  updateAccount,
  checkVerification,
  getNode,
  bendNode, getAllCommentByUsername, getAccountByPhone,
} from "../../../src/api/api";

let nowUsername= sessionStorage.getItem("nowUsername");
export default {

  data() {
    return {
      cur: 0,
      aImage: "static/image/headimage.png",
      username: "",
      messageBox: false,
      usernameExist: false,
      emailExist: false,
      phoneExist: false,
      bendByPhone: false,
      sendCode: true,
      checkBtn: false,
      cacheCode: null,
      collectmsg: false,
      canGet: false,
      timer: null,
      btnText: "Send",
      collectIsNull: "",
      user: {},
      cancelItem: null,
      collectList: [],
      commentList: [],
      commentNum: 0
    }
  },
  mounted() {
    getAccount().then((res) => {
      if (res.data.status === 0) {
        console.log(res);
        this.user = res.data.data;
        console.log(this.user.aImage);
      } else {
        console.log(res);
      }

    }).catch((res) => {
      alert("错误：" + res);
    });

    let nowUsername = sessionStorage.getItem("nowUsername");
    getAllCollectbyId(nowUsername).then((res) => {

      if (res.data.status === 0) {
        console.log("lll:")
        console.log(res.data.data);
        console.log(res.data.data.length);
        let i;
        for (i = 0; i <= res.data.data.length - 1; i++) {
          this.collectList[i] = res.data.data[i];
          console.log("成功");
          console.log(this.collectList[i]);
        }
      } else {
        console.log(res);
        this.collectmsg = true;
        this.collectIsNull = "There's nothing in favorites yet. Go and browse!"
      }

    }).catch((res) => {
      alert("错误：" + res);
    });

    this.getCommentList();
  },
  methods: {
    checkUsername() {
      let nowUsername = sessionStorage.getItem("nowUsername");
      if (nowUsername != this.user.username) {
        checkUsername(this.user.username).then((res) => {
          if (res.data.status == 0) {
            this.usernameExist = true;
          } else {
            this.usernameExist = false;
          }
        }).catch((res) => {
          alert("错误checkusername：" + res);
        });
      }


    },
    regionChange(data) {
      console.log(data);
      this.user.city = data.province.value + data.city.value + data.area.value;
      console.log(this.city);

    },
    changeAddress() {
      updateAccount(this.user).then((res) => {
        if (res.data.status == 0) {
          this.user = res.data.data;
          alert("修改成功");
        } else if (res.data.status == 3) {
          alert(res.data.msg);
        }
      }).catch((res) => {
        alert("错误changeaddress：" + res);
      });

    },
    changeAccount() {
      this.changeAddress();
    },
    checkPhoneExist() {
      checkPhone(this.user.phone).then((res) => {
        if (res.data.status == 0) {
          this.phoneExist = true;
          this.sendCode = false;
        } else {
          this.phoneExist = false;
          this.sendCode = true;
        }
      }).catch((res) => {
        alert("错误changeaddress：" + res);
      });
    },
    timer1() {   //验证码60s内不能继续点击的函数
      if (this.time > 0) {
        this.time--;
        this.btnText = this.time + "s后重新获取"
        this.timer = setTimeout(this.timer1, 1000)
      } else {
        this.time = 0;
        this.btnText = "Send";
        this.canGet = false;
        clearTimeout(this.timer);
      }
    },
    sendVerificate() {
      this.canGet = true;  //禁用按钮，防止多次触发
      this.time = 60;  //60秒后能继续按按钮
      this.timer1();
      this.checkBtn = true;
      if (this.phoneExist === false) {
        this.bendByPhone = true;
        console.log(this.user.phone);
        bendNode(this.user.phone).then((res) => {

          if (res.data.status == 0) {
            alert(res.data.msg);
            // alert("验证码发送成功");
          } else if (res.data.status == 1) {
            alert("验证码发送失败");
            alert(res.data.msg);
          }
        }).catch((res) => {
          alert("发送验证码：" + res);
        });
      }


    },
    cancelCollect(itemId) {
      deleteItemfromCollect(nowUsername, itemId).then((res) => {
        if (res.data.status == 0) {
          location.reload();
          alert("商品已取消收藏");
        }
      });
    },
    checkVerificate() {
      if (this.user.phone == null) {
        this.messageBox = true;
        this.message = "Please enter the PhoneNumber";
      } else if (this.cacheCode == null) {
        this.messageBox = true;
        this.message = "Please enter the cacheCode";
      } else {
        checkVerification(this.cacheCode).then((res) => {

          console.log(res.data)
          console.log(res.data.status)

          if (res.data.status === 1) {
            alert(res.data.msg);
          } else if (res.data.status === 2) {
            alert(res.data.msg);
          } else {
            alert(res.data.msg);
            this.changeAddress();
          }
        }).catch((res) => {
          alert("错误34：" + res);
        });
      }
    },
    //得到该用户所有评论
    getCommentList() {
      getAllCommentByUsername(nowUsername).then((res) => {
        this.commentList = res.data.data;
        this.commentNum = this.commentList.length;
      })

  },
  logOut() {

    signOutAccount().then((res) => {

      if (res.data.status == 0) {
        location.reload();
        console.log(res.data);
        alert(res.data.msg);
        console.log(res.data.status);
      }
    }).catch((res) => {
      alert("错误34：" + res);
    });
  },
  }
}



